<?php

/**
 * 后台分类管理环境页
 * @copyright (c) 2013, Mr.Wang
 * 
 */
class CatAction extends AdminAction {

    function _initialize() {
        parent::_initialize();
        $this->Model = D("Cat");
    }

    public function index() {
        $result = $this->Model->field("*,cat_id as id,parent_id as parentid")->order(array("sort_order" => "ASC"))->select();
        import("Tree");
        $tree        = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $r) {
            if ($r['type'] == "XX") {
                $addchild = '<a href="' . U("Cat/add", array("parentid" => $r['id'], "menuid"   => $_GET['menuid'])) . '">添加餐种</a> | ';
            } elseif ($r['type'] > 1) {
                $addchild = "";
            } else {
                $addchild = '<a href="' . U("Cat/add", array("parentid"       => $r['id'], "menuid"         => $_GET['menuid'])) . '">添加子分类</a> | ';
            }
            $r['str_manage'] = $addchild . '<a href="' . U("Cat/edit", array("id"     => $r['id'], "menuid" => $_GET['menuid'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("Cat/delete", array("id"         => $r['id'], "menuid"     => $this->_get("menuid"))) . '">删除</a> ';
            $r['status'] = $r['status'] ? "显示" : "不显示";
            $array[]     = $r;
        }

        $tree->init($array);
        $str   = "<tr>
	<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input'></td>
	<td align='center'>\$id</td>
	<td >\$spacer\$name</td>
                    <td align='center'>\$status</td>
	<td align='center'>\$str_manage</td>
	</tr>";
        $areas = $tree->get_tree(0, $str);
        $this->assign("areas", $areas);
        $this->display();
    }

    /**
     *  添加
     */
    public function add() {
        if (IS_POST) {
            $parent_id = (int) $_POST['parent_id'];
            if ($parent_id != 0) {
                $parent_pid = $this->Model->where(array('cat_id' => $parent_id))->find();
                if ($parent_pid['parent_id'] != 0) {
                    $this->error("新增失败, 分类只可有两级！");
                }
            }
            if ($this->Model->create()) {
                if ($this->Model->add()) {
                    $this->success("新增成功！", U("Cat/index"));
                } else {
                    $this->error("新增失败！");
                }
            } else {
                $this->error($this->Model->getError());
            }
        } else {
            import("Tree");
            $tree     = new Tree();
            $parentid = (int) $this->_get("parentid");
            $result   = $this->Model->field("*,cat_id as id,parent_id as parentid")->where("type<=1")->select();
            foreach ($result as $r) {
                $r['selected']    = $r['id'] == $parentid ? 'selected' : '';
                $array[]          = $r;
            }
            $str              = "<option value='\$id' \$selected>\$spacer \$name</option>";
            $tree->init($array);
            $select_categorys = $tree->get_tree(0, $str);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }

    /**
     *  删除
     */
    public function delete() {
        $id    = (int) $this->_get("id");
        $count = $this->Model->where(array("parent_id" => $id))->count();
        $count      = 0;
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->Model->delete($id)) {
            $this->success("删除分类成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     *  编辑
     */
    public function edit() {
        if (IS_POST) {
            if ($parent_id != 0) {
                $parent_pid = $this->Model->where(array('cat_id' => $parent_id))->find();
                if ($parent_pid['parent_id'] != 0) {
                    $this->error("新增失败, 分类只可有两级！");
                }
            }
            if ($this->Model->create()) {
                if ($this->Model->save() !== false) {
                    $this->success("更新成功！", U("Cat/index"));
                } else {
                    $this->error("更新失败！");
                }
            } else {
                $this->error($this->Model->getError());
            }
        } else {
            import("Tree");
            $tree = new Tree();
            $id   = (int) $this->_get("id");
            $rs   = $this->Model->field("*,cat_id as id,parent_id as parentid")->where(array($this->Model->getPk() => $id))->find();

            $result = $this->Model->field("*,cat_id as id,parent_id as parentid")->where("type<=1")->select();


            foreach ($result as $r) {
                $r['selected'] = $r['id'] == $rs['parentid'] ? 'selected' : '';
                $array[]       = $r;
            }

            $str              = "<option value='\$id' \$selected>\$spacer \$name</option>";
            $tree->init($array);
            $select_categorys = $tree->get_tree(0, $str);
            $this->assign("data", $rs);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }

    //排序
    public function listorders() {
        $status = parent::listorders($this->Model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

}
